/***********************************
 * SMSCursorAdapter
 * Extension of the SimpleCursorAdapter
 * this allows us to customize the view in the listactivity
 */
package edu.csuohio.mad.ssmk.subtxt;

import java.io.InputStream;
import android.content.Context;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.provider.ContactsContract;
import android.support.v4.widget.SimpleCursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class SMSCursorAdapter extends SimpleCursorAdapter {

	private Cursor c;
	private Context context;
	private ContactSupport cs;

	public SMSCursorAdapter(Context context, int layout, Cursor c,
			String[] from, int[] to, int flags) {
		super(context, layout, c, from, to, flags);
		this.c = c;
		this.context = context;
		cs = ContactSupport.getInstance(context);
	}

	@SuppressWarnings("deprecation")
	public View getView(int pos, View v, ViewGroup parent) {
		if (v == null) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = inflater.inflate(R.layout.sms_message, null);
		}

		// this updates the cursor as required when the data changes, supposedly
		// there's a better way to do this, but alas, i was unable to find it
		this.c.requery();
		this.c.moveToPosition(pos);

		// get the sender's number and timestamp
		String number = this.c.getString(this.c.getColumnIndex("address"));
		String date = this.c.getString(this.c.getColumnIndex("date"));

		TextView tvSender = (TextView) v.findViewById(R.id.sender);
		TextView tvDate = (TextView) v.findViewById(R.id.senttime);
		// reformat and set the timestamp
		tvDate.setText(ContactSupport.reformatDate(date));

		// if the message has not been read
		if ((this.c.getString(this.c.getColumnIndex("read")))
				.contentEquals("0"))
			// show the sender name in bold
			tvSender.setTextAppearance(context, R.style.unread);
		else // otherwise, show it in normal font weight
			tvSender.setTextAppearance(context, R.style.read);

		// update the remaining contact info
		updateContactInfo(v, number);
		return (v);
	}

	private void updateContactInfo(View v, String number) {

		// create the contact in the hash map if it doesn't exist
		cs.createContact(number);

		TextView tvSender = (TextView) v.findViewById(R.id.sender);
		// get the contact's display name
		tvSender.setText(cs.getDisplayName(number));

		// If this person is in our contact list, lets see their details
		if (cs.hasContact(number)) {
			tvSender.setText(cs.getDisplayName(number));

			// Only add a photo if there is one
			if (cs.getImageUri(number) != null) {
				InputStream input = ContactsContract.Contacts
						.openContactPhotoInputStream(
								context.getContentResolver(),
								cs.getImageUri(number));
				ImageView iv = (ImageView) v.findViewById(R.id.pic);
				iv.setImageBitmap(BitmapFactory.decodeStream(input));
			} else { // otherwise, set the launcher icon
				ImageView iv = (ImageView) v.findViewById(R.id.pic);
				iv.setImageDrawable(context.getResources().getDrawable(
						R.drawable.ic_launcher));
			}
		}
	}
}